import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../config/public_color.dart';
import '../../config/public_text.dart';
import '../../foundation/style/normal_text_style.dart';
import '../../tool/public_tool.dart';
import '../button/border_button.dart';
import '../container/separated_column.dart';
import '../image/assets_image.dart';

/// FileName empty_indicator
///
/// @Author lax
/// @Date 2023/4/25
///
/// @Description

class EmptyIndicator extends StatelessWidget {
  static TextStyle? defaultTextStyle;

  static Widget? defaultRefreshWidget;

  static Widget? defaultImage;

  late final String? text;

  late final TextStyle? textStyle;

  late final Widget? image;

  final String? imageUrl;

  late final String? refreshText;

  late final Widget? refreshWidget;

  /// 不为null时显示刷新按钮
  final VoidCallback? onRefresh;

  final double? width;

  final double? height;

  final Widget? separator;

  EmptyIndicator({
    super.key,
    String? text,
    TextStyle? textStyle,
    Widget? image,
    this.imageUrl,
    String? refreshText,
    Widget? refreshWidget,
    this.onRefresh,
    this.width,
    this.height,
    this.separator,
  }) {
    this.text = text ?? PublicText.noData;
    this.textStyle = textStyle ?? defaultTextStyle;
    this.image =
        image ?? (imageUrl == null ? defaultImage : AssetsImage(url: imageUrl));
    this.refreshText = refreshText ?? PublicText.refresh;
    this.refreshWidget = refreshWidget ?? defaultRefreshWidget;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width,
      height: height,
      alignment: Alignment.center,
      child: SeparatedColumn(
        separator: separator ?? const SizedBox(height: 16),
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Visibility(
            visible: image != null,
            child: image ?? const SizedBox(),
          ),
          Visibility(
            visible: stringIsNotEmpty(text),
            child: Text(
              text ?? '',
              style: textStyle ?? TextStyle(color: PublicColor.hintText),
            ),
          ),
          Visibility(
            visible: onRefresh != null,
            child: refreshWidget ??
                UnconstrainedBox(
                  child: BorderButton(
                    padding:
                        const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
                    text: refreshText,
                    textStyle: NormalTextStyle(
                        fontSize: 12, color: PublicColor.lightText),
                    onPressed: () {
                      if (onRefresh != null) {
                        onRefresh!();
                      }
                    },
                  ),
                ),
          ),
        ],
      ),
    );
  }
}
